package com.cheifsteam.springframework.context.support;

import com.cheifsteam.springframework.beans.BeansException;
import com.cheifsteam.springframework.beans.factory.config.BeanPostProcessor;
import com.cheifsteam.springframework.context.ApplicationContext;
import com.cheifsteam.springframework.context.ApplicationContextAware;

/**
 * @author 屈燃希
 * @date 2025/2/15 20:11
 * @content
 */
public class ApplicationContextAwareProcessor implements BeanPostProcessor {


    private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ApplicationContextAware)
            ((ApplicationContextAware) bean).setApplicationContext(applicationContext);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
